const { Train } = require("../model");

// 改
exports.updateTrain = async (req, res, next) => {
  try {
    const fun = await Train.findByIdAndUpdate(req.params.id, req.body)
    if (fun) {
      res.status(200).json({
        code: 200,
        msg: "success",
      });
    } else {
      res.status(200).json({
        code: 500,
        msg: "修改失败",
      });
    }
    
  } catch (error) {
    next(error);
  }
};

// 改某一属性
exports.updateTrainParams = async (req, res, next) => {
  try {
    const fun = await Train.findByIdAndUpdate(req.params.id, {
      $set: req.body
    })
    if (fun) {
      res.status(200).json({
        code: 200,
        msg: "success",
      });
    } else {
      res.status(200).json({
        code: 500,
        msg: "修改失败",
      });
    }
    
  } catch (error) {
    next(error);
  }
};
// 搜索景点
exports.getTrain = async (req, res, next) => {
  try {
    const items = await Train.find();
    // 获取fun的评论
    res.status(200).json({
      code: 200,
      data: items
    });
  } catch (error) {
    next(error);
  }
};
// 通过id查找
exports.getTrainById = async (req, res, next) => {
  try {
    const items = await Train.findById(req.params.id);
    res.status(200).json({
      code: 200,
      data: items
    });
  } catch (error) {
    next(error);
  }
};
// 增
exports.addTrain = async (req, res, next) => {
  try {
    const model = new Train(req.body);
    await model.save();
    res.status(200).json({
      code: 200,
      msg: "success",
    });
  } catch (error) {
    next(error);
  }
};
// 删
exports.deleteTrain = async (req, res, next) => {
  try {
    const rs = await Train.findByIdAndDelete(req.params.id)
    if (rs) {
      res.status(200).json({
        code: 200,
        msg: '删除成功'
      });
    }
  } catch (error) {
    next(error);
  }
};

